﻿@using EmployeeLeaveSystem.Shared;
@using EmployeeLeaveSystem.Shared.Models;
@inject IMessageService _MessageService;
@inject HttpClient _HttpClient;
@inject ModalService _modalService;
@inject ModalService _modalService;

<h5>我所应该发放的工资</h5>
<p>我的下属列表</p>
<AntList Class="demo-loadmore-list" DataSource="@listPeoples" ItemLayout="ListItemLayout.Horizontal">
    <ChildContent Context="item">
        <ListItem>
            <ListItemMeta Description="@item.Description">
                <AvatarTemplate>
                    <Avatar Src="@("ImgsReq/"+item.Avatar_Name)" />
                </AvatarTemplate>
                <TitleTemplate>
                    <a>@item.Name</a>
                </TitleTemplate>
            </ListItemMeta>
            <a @onclick="()=>{}">查看</a>
        </ListItem>
    </ChildContent>
</AntList>
<p>需要我审核的</p>
<Table DataSource="listNeedAcceptInfos"
       @bind-SelectedRows="selectedRows">
    <Selection />
    <PropertyColumn Property="x=>x.Id" Sortable />
    <PropertyColumn Property="x=>x.Staff.Name" />
    <PropertyColumn Property="x=>x.Year" />
    <PropertyColumn Property="x=>x.Month" />
    <PropertyColumn Property="x=>x.BaseSalary" />
    <PropertyColumn Property="x=>x.AddLiveSalary" />
    <PropertyColumn Property="x=>x.CutOffSalary" />
    <PropertyColumn Property="x=>x.ActualSalary" />
</Table>
@if (selectedRows != null && selectedRows.Any()) {
    <Button Danger Size="small" OnClick="@(e => { selectedRows = null; })">Clear</Button>
    <div>当前需要您批准的：</div>
    @foreach (var selected in selectedRows) {
        <div>
            <Descriptions>
                <DescriptionsItem Title="编号">@selected.Id</DescriptionsItem>
                <DescriptionsItem Title="年份">@selected.Year</DescriptionsItem>
                <DescriptionsItem Title="月份">@selected.Month</DescriptionsItem>
                <DescriptionsItem Title="基础薪资">
                    <Input @bind-Value="@selected.BaseSalary" />
                </DescriptionsItem>
                <DescriptionsItem Title="奖励薪水">
                    <Input @bind-Value="@selected.AddLiveSalary" />
                </DescriptionsItem>
                <DescriptionsItem Title="扣除薪水">
                    <Input @bind-Value="@selected.CutOffSalary" />
                </DescriptionsItem>
            </Descriptions>
            <div>浮动金额</div>
            <Input @bind-Value="selected.CustomSalary" />
            <div>原因说明</div>
            <TextArea @bind-Value="selected.Reason" />
            <div>审批意见选择</div>
            <EnumSelect @bind-Value="selected.AcceptFlag" />
            <p>最终金额：@selected.ActualSalary</p>
            <Divider />
        </div>
    }

    double totalSalarys = 0;
    foreach (var item in selectedRows) {
        totalSalarys += item.ActualSalary;
    }

    <div>本部门总计发放工资：@totalSalarys</div>
    <Button Block Type="primary" OnClick="AddSalaryInfo">提交更新</Button>
}
<p />
<h5>所有的历史记录</h5>
<Table DataSource="listSalaryInfos"
       @bind-SelectedRows="selectedRows">
    <PropertyColumn Property="x=>x.Id" Sortable />
    <PropertyColumn Property="x=>x.Staff.Name" />
    <PropertyColumn Property="x=>x.Year" />
    <PropertyColumn Property="x=>x.Month" />
    <PropertyColumn Property="x=>x.BaseSalary" />
    <PropertyColumn Property="x=>x.AddLiveSalary" />
    <PropertyColumn Property="x=>x.CutOffSalary" />
    <PropertyColumn Property="x=>x.ActualSalary" />
</Table>
@code {
    // 用于展示自己的人员列表
    List<StaffInfo> listPeoples = new();
    static RenderFragment edit = @<a key="list-loadmore-edit">edit</a>;
    protected override async Task OnInitializedAsync() {
        // 获取自己的下属列表
        listPeoples = await _HttpClient.GetFromJsonAsync<List<StaffInfo>>($"api/Auth/GetAllChilden?id={GobalManager.myInfo.Id}");
        GobalManager.myInfo.childStaffs = listPeoples;

        // 获取所有需要我批准的薪水发布记录
        listSalaryInfos = await _HttpClient.GetFromJsonAsync<List<SalaryInfo>>($"api/Salary/GetSalaryInfosManagerId?id={GobalManager.myInfo.Id}");
        foreach (var item in listSalaryInfos) {
            if (item.AcceptFlag == AcceptFlag.审核中) {
                listNeedAcceptInfos.Add(item);
            }
        }

        InvokeAsync(StateHasChanged);
    }
    // 薪水审核记录
    List<SalaryInfo> listSalaryInfos = new();
    List<SalaryInfo> listNeedAcceptInfos = new();
    IEnumerable<SalaryInfo> selectedRows;

    async void AddSalaryInfo() {
        await _HttpClient.PostAsJsonAsync("api/Salary/UpdateSalaryInfos", selectedRows);
        await _MessageService.Success("加载完成");
    }
}
